package com.hutao.controller;

import com.hutao.pojo.Product;
import com.hutao.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param cid
     * @param pid
     * @param numbers
     * @param model
     * @return
     */
    @RequestMapping("addCart")
    public String addCart(int cid, int pid, int numbers, Model model){
        //添加购物车
        cartService.insertCart(cid,pid,numbers);
        //获取当前的登录人的购物车的商品信息
        List<Product> productList =  cartService.getProduct(cid);
        model.addAttribute("productList",productList);
        return "forward:/cart.jsp";//webapp/cart.jsp
    }

    /**
     *
     * @param cid
     * @param productId
     * @return
     */
    @RequestMapping("delCart")
    public String delCart(int cid,int productId,Model model){
        cartService.delCart(cid,productId);
        //获取当前的登录人的购物车的商品信息
        List<Product> productList =  cartService.getProduct(cid);
        model.addAttribute("productList",productList);
        return "forward:/cart.jsp";//webapp/cart.jsp
    }

    /**
     * 获取当前的登录人的购物车
     * @param cid
     * @param model
     * @return
     */
    @RequestMapping("getCart")
    public String getCart(int cid,Model model){
        //获取当前的登录人的购物车的商品信息
        List<Product> productList =  cartService.getProduct(cid);
        model.addAttribute("productList",productList);
        return "forward:/cart.jsp";//webapp/cart.jsp
    }


}
